{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Check if Every Row and Column Contains All Numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: checkValid"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #检查是否每一行每一列都包含全部整数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>对一个大小为 <code>n x n</code> 的矩阵而言，如果其每一行和每一列都包含从 <code>1</code> 到 <code>n</code> 的 <strong>全部</strong> 整数（含 <code>1</code> 和 <code>n</code>），则认为该矩阵是一个 <strong>有效</strong> 矩阵。</p>\n",
    "\n",
    "<p>给你一个大小为 <code>n x n</code> 的整数矩阵 <code>matrix</code> ，请你判断矩阵是否为一个有效矩阵：如果是，返回 <code>true</code> ；否则，返回 <code>false</code> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/12/21/example1drawio.png\" style=\"width: 250px; height: 251px;\" /></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>matrix = [[1,2,3],[3,1,2],[2,3,1]]\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释：</strong>在此例中，n = 3 ，每一行和每一列都包含数字 1、2、3 。\n",
    "因此，返回 true 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<p><img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/12/21/example2drawio.png\" style=\"width: 250px; height: 251px;\" /></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>matrix = [[1,1,1],[1,2,3],[1,2,3]]\n",
    "<strong>输出：</strong>false\n",
    "<strong>解释：</strong>在此例中，n = 3 ，但第一行和第一列不包含数字 2 和 3 。\n",
    "因此，返回 false 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>n == matrix.length == matrix[i].length</code></li>\n",
    "\t<li><code>1 &lt;= n &lt;= 100</code></li>\n",
    "\t<li><code>1 &lt;= matrix[i][j] &lt;= n</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [check-if-every-row-and-column-contains-all-numbers](https://leetcode.cn/problems/check-if-every-row-and-column-contains-all-numbers/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [check-if-every-row-and-column-contains-all-numbers](https://leetcode.cn/problems/check-if-every-row-and-column-contains-all-numbers/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[[1,2,3],[3,1,2],[2,3,1]]', '[[1,1,1],[1,2,3],[1,2,3]]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        occur = set()\n",
    "\n",
    "        n = len(matrix)\n",
    "\n",
    "        for i in range(n):\n",
    "            occur.clear()\n",
    "            for j in range(n):\n",
    "                if matrix[i][j] in occur:\n",
    "                    return False\n",
    "                else:\n",
    "                    occur.add(matrix[i][j])\n",
    "\n",
    "        for i in range(n):\n",
    "            occur.clear()\n",
    "            for j in range(n):\n",
    "                if matrix[j][i] in occur:\n",
    "                    return False\n",
    "                else:\n",
    "                    occur.add(matrix[j][i])\n",
    "\n",
    "        return True\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        return all(len(set(row)) == len(matrix) for row in matrix) and all(len(set(col)) == len(matrix) for col in zip(*matrix))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        n = len(matrix)\n",
    "        occur = set()   # 每一行/列出现过的整数\n",
    "        # 判断每一行是否符合要求\n",
    "        for i in range(n):\n",
    "            occur.clear()   # 确保统计前哈希表为空\n",
    "            for j in range(n):\n",
    "                if matrix[i][j] in occur:\n",
    "                    # 出现重复整数，该行不符合要求\n",
    "                    return False\n",
    "                occur.add(matrix[i][j])\n",
    "        # 判断每一列是否符合要求\n",
    "        for i in range(n):\n",
    "            occur.clear()   # 确保统计前哈希表为空\n",
    "            for j in range(n):\n",
    "                if matrix[j][i] in occur:\n",
    "                    # 出现重复整数，该列不符合要求\n",
    "                    return False\n",
    "                occur.add(matrix[j][i])\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        n = len(matrix)\n",
    "        for row, col in zip(matrix, zip(*matrix)):\n",
    "            if len(set(row)) != n or len(set(col)) != n:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        n=len(matrix)\n",
    "        for i in range(n):\n",
    "            if len(set(matrix[i]))!=n:\n",
    "                return False\n",
    "        a=[]\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                a.append(matrix[j][i])\n",
    "            if len(set(a))!=n:\n",
    "                return False\n",
    "            #a.clear()\n",
    "            a=[]\n",
    "        return True\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        l = len(matrix)\n",
    "\n",
    "        # check row\n",
    "        for i in range(l):\n",
    "            if len(set(matrix[i])) != l:\n",
    "                return False\n",
    "\n",
    "        # check column\n",
    "        for i in range(l):\n",
    "            col = []\n",
    "            for j in range(l):\n",
    "                col.append(matrix[j][i])\n",
    "            if len(set(col)) != l:\n",
    "                return False\n",
    "\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        n = len(matrix)\n",
    "\n",
    "        target = set(range(1, n + 1))\n",
    "\n",
    "        for row in matrix:\n",
    "            if set(row) != target:\n",
    "                return False\n",
    "        \n",
    "        for col in zip(*matrix):\n",
    "            if set(col) != target:\n",
    "                return False\n",
    "\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import copy\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        n = len(matrix)\n",
    "        order_list = list(range(1, n + 1))\n",
    "        # print(range(1, n + 1))\n",
    "        for line in matrix:\n",
    "            _line = copy.deepcopy(line)\n",
    "            _line.sort()\n",
    "            # print(_line)\n",
    "            if _line != order_list:\n",
    "                return False\n",
    "\n",
    "        for i in range(n):\n",
    "            col = []\n",
    "            # print(f'{i} column')\n",
    "            for j in range(n):\n",
    "                num = matrix[j][i]\n",
    "                col.append(num)\n",
    "            col.sort()\n",
    "            if col != order_list:\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        n = len(matrix)\n",
    "        s = set(i + 1 for i in range(n))\n",
    "        return all(set(matrix[i][j] for j in range(n)) == s for i in range(n)) and all(set(matrix[i][j] for i in range(n)) == s for j in range(n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        n = len(matrix)\n",
    "        occur = set()   # 每一行/列出现过的整数\n",
    "        # 判断每一行是否符合要求\n",
    "        for i in range(n):\n",
    "            occur.clear()   # 确保统计前哈希表为空\n",
    "            for j in range(n):\n",
    "                if matrix[i][j] in occur:\n",
    "                    # 出现重复整数，该行不符合要求\n",
    "                    return False\n",
    "                occur.add(matrix[i][j])\n",
    "        # 判断每一列是否符合要求\n",
    "        for i in range(n):\n",
    "            occur.clear()   # 确保统计前哈希表为空\n",
    "            for j in range(n):\n",
    "                if matrix[j][i] in occur:\n",
    "                    # 出现重复整数，该列不符合要求\n",
    "                    return False\n",
    "                occur.add(matrix[j][i])\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        n = len(matrix)\n",
    "        occur = set()\n",
    "        for i in range(n):\n",
    "            occur.clear()\n",
    "            for j in range(n):\n",
    "                if matrix[i][j] in occur:\n",
    "                    return False\n",
    "                occur.add(matrix[i][j])\n",
    "        for i in range(n):\n",
    "            occur.clear()\n",
    "            for j in range(n):\n",
    "                if matrix[j][i] in occur:\n",
    "                    return False\n",
    "                occur.add(matrix[j][i])\n",
    "        return True\n",
    "        \n",
    "        \n",
    "        \n",
    "         \n",
    "        \n",
    "        \n",
    "        # 解答错误 219 / 227 个通过的测试用例\n",
    "        # for i in range(n):\n",
    "        #     setMa = set(matrix[i])\n",
    "        #     print(setMa)\n",
    "        #     if len(setMa) != n:\n",
    "        #         return False  \n",
    "        # return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        def check_row(row, n):\n",
    "            copy = [item for item in row]\n",
    "            copy.sort()\n",
    "            for i in range(1, n+1):\n",
    "                if copy[i-1] != i:\n",
    "                    return False\n",
    "            return True\n",
    "        rows, cols = matrix.__len__(), matrix[0].__len__()\n",
    "        for i in range(rows):\n",
    "            if check_row(matrix[i], rows) == False:\n",
    "                return False\n",
    "        # 检查列\n",
    "        for j in range(cols):\n",
    "            tmp = []\n",
    "            for i in range(rows):\n",
    "                tmp.append(matrix[i][j])\n",
    "            if check_row(tmp, cols) == False:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        a=[i for i in range(1,len(matrix)+1)]\n",
    "        b=[[]for i in range(len(matrix))]\n",
    "        for i in range(len(matrix)):\n",
    "            for x in range(len(matrix)):\n",
    "                b[i].append(matrix[x][i])          \n",
    "        for i in range(len(matrix)):\n",
    "            p=matrix[i]\n",
    "            q=b[i]\n",
    "            p.sort()\n",
    "            q.sort()\n",
    "            if p!=a or q!=a:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        for row in matrix:\n",
    "            if len(set(row)) != len(matrix):\n",
    "                return False\n",
    "        for col in (set(matrix[row_i][col_i] for row_i in range(len(matrix))) for col_i in range(len(matrix))):\n",
    "            if len(col) != len(matrix):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        cur = [[] for _ in range(len(matrix))]\n",
    "        for x in matrix:\n",
    "            for j, y in enumerate(x):\n",
    "                if y in x[:j] or y in cur[j]:\n",
    "                    return False\n",
    "                cur[j].append(y)\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        n = len(matrix)\n",
    "        for i in range(n):\n",
    "            helper1 = set()\n",
    "            helper2 = set()\n",
    "            for j in range(n):\n",
    "                if matrix[i][j] in helper1 or matrix[j][i] in helper2:\n",
    "                    return False\n",
    "                helper1.add(matrix[i][j])\n",
    "                helper2.add(matrix[j][i])\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        return self.is_valid_matrix(matrix)\n",
    "\n",
    "\n",
    "    def is_valid_matrix(self, matrix):\n",
    "        n = len(matrix)\n",
    "        \n",
    "        # 检查每一行\n",
    "        for row in matrix:\n",
    "            if len(set(row)) != n or min(row) != 1 or max(row) != n:\n",
    "                return False\n",
    "        \n",
    "        # 检查每一列\n",
    "        for col in range(n):\n",
    "            column = [matrix[i][col] for i in range(n)]\n",
    "            if len(set(column)) != n or min(column) != 1 or max(column) != n:\n",
    "                return False\n",
    "        \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        for x in range(len(matrix)):\n",
    "            row_tag = set()\n",
    "            col_tag = set()\n",
    "            for y in range(len(matrix)):\n",
    "                if matrix[x][y] in row_tag:\n",
    "                    return False\n",
    "                if matrix[y][x] in col_tag:\n",
    "                    return False\n",
    "                row_tag.add(matrix[x][y])\n",
    "                col_tag.add(matrix[y][x])\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        n = len(matrix)\n",
    "        return all(len(set(x)) == n for x in matrix) and all([ len(set([matrix[j][i] for j in range(n)])) == n for i in range(n)])\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        n = len(matrix)\n",
    "        s = set(i + 1 for i in range(n))\n",
    "        for i in range(n):\n",
    "            if set(matrix[i][j] for j in range(n)) != s:\n",
    "                return False\n",
    "        for j in range(n):\n",
    "            if set(matrix[i][j] for i in range(n)) != s:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        matrix1 = [[matrix[i][j] for i in range(len(matrix))] for j in range(len(matrix[0]))]\n",
    "        for i in matrix:\n",
    "            i.sort()\n",
    "            res = []\n",
    "            for j in range(1,len(i)+1):\n",
    "                res.append(j)\n",
    "            if i != res:    \n",
    "                return False\n",
    "        for i in matrix1:\n",
    "            i.sort()\n",
    "            res = []\n",
    "            for j in range(1,len(i)+1):\n",
    "                res.append(j)\n",
    "            if i != res:    \n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        ls = set()\n",
    "        n = len(matrix)\n",
    "        for i in range(0,n):\n",
    "            for j in range(0,n):\n",
    "                ls.add(matrix[i][j])\n",
    "            if len(ls) == n:\n",
    "                ls.clear()\n",
    "                continue\n",
    "\n",
    "            else:return False\n",
    "\n",
    "        ls.clear()\n",
    "        for i in range(0,n):\n",
    "            for j in range(0,n):\n",
    "                ls.add(matrix[j][i])\n",
    "            if len(ls) == n:\n",
    "                ls.clear()\n",
    "                continue\n",
    "            else:return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        n = len(matrix)\n",
    "        occur = set()   # 每一行/列出现过的整数\n",
    "        # 判断每一行是否符合要求\n",
    "        for i in range(n):\n",
    "            occur.clear()   # 确保统计前哈希表为空\n",
    "            for j in range(n):\n",
    "                if matrix[i][j] in occur:\n",
    "                    # 出现重复整数，该行不符合要求\n",
    "                    return False\n",
    "                occur.add(matrix[i][j])\n",
    "        # 判断每一列是否符合要求\n",
    "        for i in range(n):\n",
    "            occur.clear()   # 确保统计前哈希表为空\n",
    "            for j in range(n):\n",
    "                if matrix[j][i] in occur:\n",
    "                    # 出现重复整数，该列不符合要求\n",
    "                    return False\n",
    "                occur.add(matrix[j][i])\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        n = len(matrix)\n",
    "        for i in range(n):\n",
    "            if sorted(matrix[i])!= list(range(1, n+1)):\n",
    "                return False\n",
    "        for j in range(n):\n",
    "            column = [matrix[i][j] for i in range(n)]\n",
    "            if sorted(column)!= list(range(1, n+1)):\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        n=len(matrix)\n",
    "        occur = set()\n",
    "        for i in range(n):\n",
    "            occur.clear()\n",
    "            for j in range(n):\n",
    "                if matrix[i][j] in occur:\n",
    "                    return False\n",
    "                occur.add(matrix[i][j])\n",
    "        for i in range(n):\n",
    "            occur.clear()\n",
    "            for j in range(n):\n",
    "                if matrix[j][i] in occur:\n",
    "                    return False\n",
    "                occur.add(matrix[j][i])\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        s = set((x for x in range(1, len(matrix) + 1)))\n",
    "        for row in matrix:\n",
    "            if set(row) != s:\n",
    "                return False\n",
    "        for col_i in range(len(matrix)):\n",
    "            temp_s = set()\n",
    "            for row_i in range(len(matrix)):\n",
    "                temp_s.add(matrix[row_i][col_i])\n",
    "            if temp_s != s:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        def check(ls):\n",
    "            size = len(ls)\n",
    "            cal = [0] * size\n",
    "            for n in ls:\n",
    "                cal[n % size] = 1\n",
    "            return sum(cal) == size\n",
    "\n",
    "        for line in matrix:\n",
    "            if not check(line):\n",
    "                return False\n",
    "\n",
    "        for line in list(zip(*matrix)):\n",
    "            if not check(line):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        # arr = [i+1 for i in len(matrix)]\n",
    "        \n",
    "        for i in range(len(matrix)):\n",
    "            ans = [i+1 for i in range(len(matrix))]\n",
    "            for j in range(len(matrix)):\n",
    "                if matrix[i][j] in ans:\n",
    "                    ans.remove(matrix[i][j])\n",
    "            if len(ans)==0:\n",
    "                continue\n",
    "            else:\n",
    "                return False \n",
    "        \n",
    "        for i in range(len(matrix)):\n",
    "            ans = [i+1 for i in range(len(matrix))]\n",
    "            for j in range(len(matrix)):\n",
    "                if matrix[j][i] in ans:\n",
    "                    ans.remove(matrix[j][i])\n",
    "            if len(ans)==0:\n",
    "                continue\n",
    "            else:\n",
    "                return False \n",
    "            \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool: \n",
    "        n = len(matrix)\n",
    "        for row, col in zip(matrix, zip(*matrix)):\n",
    "            if len(set(row)) != n or len(set(col)) != n:\n",
    "                return False\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        num = set(i for i in range(1,len(matrix)+1))\n",
    "        for row in matrix:\n",
    "            if set(row) != num:\n",
    "                return False\n",
    "        for column in zip(*matrix):\n",
    "            if set(column) != num:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        return all(len(set(row)) == len(matrix) for row in matrix + list(zip(*matrix)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        num = set(i for i in range(1,len(matrix)+1))\n",
    "        for row in matrix:\n",
    "            if set(row) != num:\n",
    "                return False\n",
    "        for column in zip(*matrix):\n",
    "            if set(column) != num:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        n=len(matrix)\n",
    "        for row,column in zip(matrix,zip(*matrix)):\n",
    "            if len(set(row))!=n or len(set(column))!=n:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        ###copy matrix\n",
    "        ct=[[None for _ in range(len(matrix[0]))] for _ in range(len(matrix))]\n",
    "        for i in range(len(matrix[0])):\n",
    "            for j in range(len(matrix)):\n",
    "                ct[i][j]=matrix[i][j]\n",
    "        flag=True\n",
    "        tr=[]\n",
    "        ###check row\n",
    "        for i in range(len(ct)):\n",
    "            ct[i].sort()\n",
    "            for j in range(len(ct[i])):\n",
    "                if j-1>=0 and ct[i][j]<=ct[i][j-1]:\n",
    "                    return False\n",
    "        ###check col\n",
    "        for i in range(len(matrix[0])):\n",
    "            t=[]\n",
    "            for j in range(len(matrix)):\n",
    "                t.append(matrix[j][i])\n",
    "            tr.append(t)\n",
    "        #print(tr)\n",
    "        for i in tr:\n",
    "            i.sort()\n",
    "            for j in range(len(i)):\n",
    "                if j-1>=0 and i[j]<=i[j-1]:\n",
    "                    return False\n",
    "        return flag"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        n = len(matrix)\n",
    "        def check(nums):\n",
    "            return sorted(nums) == [i for i in range(1, n+1)]\n",
    "        \n",
    "        for row in matrix:\n",
    "            if not check(row):\n",
    "                return False\n",
    "        \n",
    "        for col in zip(*matrix):\n",
    "            col = list(col)\n",
    "            if not check(col):\n",
    "                return False\n",
    "        \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        n = len(matrix)\n",
    "        for row in matrix:\n",
    "            s = set(row)\n",
    "            if any(i not in s for i in range(1, n + 1)):\n",
    "                return False\n",
    "        \n",
    "        for j in range(n):\n",
    "            s = set(matrix[i][j] for i in range(n))\n",
    "            if any(i not in s for i in range(1, n + 1)):\n",
    "                return False\n",
    "                \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        ct=[[None for _ in range(len(matrix[0]))] for _ in range(len(matrix))]\n",
    "        for i in range(len(matrix[0])):\n",
    "            for j in range(len(matrix)):\n",
    "                ct[i][j]=matrix[i][j]\n",
    "        flag=True\n",
    "        tr=[]\n",
    "        for i in range(len(ct)):\n",
    "            ct[i].sort()\n",
    "            for j in range(len(ct[i])):\n",
    "                if j-1>=0 and ct[i][j]<=ct[i][j-1]:\n",
    "                    return False\n",
    "\n",
    "        for i in range(len(matrix[0])):\n",
    "            t=[]\n",
    "            for j in range(len(matrix)):\n",
    "                t.append(matrix[j][i])\n",
    "            tr.append(t)\n",
    "        #print(tr)\n",
    "        for i in tr:\n",
    "            i.sort()\n",
    "            for j in range(len(i)):\n",
    "                if j-1>=0 and i[j]<=i[j-1]:\n",
    "                    return False\n",
    "        return flag"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        n = len(matrix)\n",
    "        occur = set()   # 每一行/列出现过的整数\n",
    "        # 判断每一行是否符合要求\n",
    "        for i in range(n):\n",
    "            occur.clear()   # 确保统计前哈希表为空\n",
    "            for j in range(n):\n",
    "                if matrix[i][j] in occur:\n",
    "                    # 出现重复整数，该行不符合要求\n",
    "                    return False\n",
    "                occur.add(matrix[i][j])\n",
    "        # 判断每一列是否符合要求\n",
    "        for i in range(n):\n",
    "            occur.clear()   # 确保统计前哈希表为空\n",
    "            for j in range(n):\n",
    "                if matrix[j][i] in occur:\n",
    "                    # 出现重复整数，该列不符合要求\n",
    "                    return False\n",
    "                occur.add(matrix[j][i])\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        n=len(matrix)\n",
    "        for i in range(n):\n",
    "            if len(set(matrix[i]))!=n:\n",
    "                return False\n",
    "        a=[]\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                a.append(matrix[j][i])\n",
    "            if len(set(a))!=n:\n",
    "                return False\n",
    "            a.clear()\n",
    "        return True\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        n = len(matrix)\n",
    "\n",
    "        target = list(range(1, n + 1))\n",
    "\n",
    "        for row in matrix:\n",
    "            if sorted(row) != target:\n",
    "                return False\n",
    "        \n",
    "        for col in zip(*matrix):\n",
    "            if sorted(col) != target:\n",
    "                return False\n",
    "\n",
    "        return True\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        n = len(matrix)\n",
    "        s = set([i for i in range(1, n + 1)])\n",
    "        for i in range(n):\n",
    "            if set(matrix[i]) != s:\n",
    "                return False\n",
    "        for j in range(n):\n",
    "            ans = set()\n",
    "            for i in range(n):\n",
    "                ans.add(matrix[i][j])\n",
    "            if ans != s:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "\n",
    "        n = len(matrix)\n",
    "\n",
    "        for i in range(n):\n",
    "            if len(set(matrix[i]))!=n:\n",
    "                return False\n",
    "        \n",
    "        for j in range(n):\n",
    "            oneCol = []\n",
    "            for i in range(n):\n",
    "                oneCol.append(matrix[i][j])\n",
    "            \n",
    "            if len(set(oneCol))!=n:\n",
    "                return False\n",
    "        \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        n = len(matrix)\n",
    "        occur = set()   # 每一行/列出现过的整数\n",
    "        # 判断每一行是否符合要求\n",
    "        for i in range(n):\n",
    "            occur.clear()   # 确保统计前哈希表为空\n",
    "            for j in range(n):\n",
    "                if matrix[i][j] in occur:\n",
    "                    # 出现重复整数，该行不符合要求\n",
    "                    return False\n",
    "                occur.add(matrix[i][j])\n",
    "        # 判断每一列是否符合要求\n",
    "        for i in range(n):\n",
    "            occur.clear()   # 确保统计前哈希表为空\n",
    "            for j in range(n):\n",
    "                if matrix[j][i] in occur:\n",
    "                    # 出现重复整数，该列不符合要求\n",
    "                    return False\n",
    "                occur.add(matrix[j][i])\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        num = set(i for i in range(1,len(matrix)+1))\n",
    "        for row in matrix:\n",
    "            if set(row) != num:\n",
    "                return False\n",
    "        for column in zip(*matrix):\n",
    "            if set(column) != num:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        n=len(matrix)\n",
    "        dic=set([i for i in range(1,n+1)])\n",
    "        for i in range(n):\n",
    "            if set(matrix[i])!=dic:\n",
    "                return False\n",
    "        for j in range(n):\n",
    "            if set([matrix[t][j] for t in range(n)])!=dic:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool: \n",
    "        return all(sorted(l) == list(range(1, len(matrix) + 1)) for l in matrix + list(zip(*matrix)))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        n = len(matrix)\n",
    "\n",
    "        for i in range(n):\n",
    "            Tlist = [False]*n\n",
    "            for j in range(n):\n",
    "                Tlist[matrix[i][j]-1]=True\n",
    "            for i in Tlist:\n",
    "                if i != True:\n",
    "                    return False\n",
    "\n",
    "        for j in range(n):\n",
    "            Tlist = [False]*n\n",
    "            for i in range(n):\n",
    "                Tlist[matrix[i][j]-1]=True\n",
    "            for i in Tlist:\n",
    "                if i != True:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        n = len(matrix)\n",
    "        cols = [[]]\n",
    "        for line in matrix:\n",
    "            for i in range(n):\n",
    "                if cols[i]:\n",
    "                    cols[i].append(line[i])\n",
    "                else:\n",
    "                    cols.append([])\n",
    "                    cols[i].append(line[i])\n",
    "                if i + 1 in line:\n",
    "                    continue\n",
    "                else:\n",
    "                    return False\n",
    "        cols.remove([])\n",
    "        for col in cols:\n",
    "            for j in range(n):\n",
    "                if j + 1 in col:\n",
    "                    continue\n",
    "                else:\n",
    "                    return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        num = set(i for i in range(1,len(matrix)+1))\n",
    "        for row in matrix:\n",
    "            if set(row) != num:\n",
    "                return False\n",
    "        for column in zip(*matrix):\n",
    "            if set(column) != num:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        def judge(ns,n):\n",
    "            return sorted(ns) == [x for x in range(1,n+1)]\n",
    "        \n",
    "        n = len(matrix)\n",
    "        for r in range(n):\n",
    "            t = [matrix[c][r] for c in range(n)]\n",
    "            if not judge(matrix[r],n):\n",
    "                return False\n",
    "            if not judge(t,n):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        rowNum = len(matrix)\n",
    "        colNum = len(matrix[0])\n",
    "        # 遍历每列\n",
    "        for rows in matrix:\n",
    "            res = [False] * (colNum + 1)\n",
    "            for nums in rows:\n",
    "                if res[nums]:\n",
    "                    return False\n",
    "                else:\n",
    "                    res[nums] = True\n",
    "        # 遍历每行\n",
    "        for i in range(0, colNum):\n",
    "            res = [False] * (rowNum + 1)\n",
    "            for j in range(0, rowNum):\n",
    "                if res[matrix[j][i]]:\n",
    "                    return False\n",
    "                else:\n",
    "                    res[matrix[j][i]] = True\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        num = set(i for i in range(1,len(matrix)+1))\n",
    "        for row in matrix:\n",
    "            if set(row) != num:\n",
    "                return False\n",
    "        for column in zip(*matrix):\n",
    "            if set(column) != num:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        n=len(matrix)\n",
    "        s=set()\n",
    "        for i in range(1,n+1):\n",
    "            s.add(i)\n",
    "        for i in range(n):\n",
    "            if set(matrix[i])!=s:\n",
    "                return False\n",
    "        matrixT=list(zip(*matrix))\n",
    "        for i in range(n):\n",
    "            if set(matrixT[i])!=s:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        def judge(ns,n):\n",
    "            return sorted(ns) == [x for x in range(1,n+1)]\n",
    "        \n",
    "        n = len(matrix)\n",
    "        for r in range(n):\n",
    "            t = [matrix[c][r] for c in range(n)]\n",
    "            if not judge(matrix[r],n):\n",
    "                return False\n",
    "            if not judge(t,n):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, m: List[List[int]]) -> bool:\n",
    "        n = len(m)\n",
    "        for i in range(n):\n",
    "            r,c = set(),set()\n",
    "            for j in range(n):\n",
    "                r.add(m[i][j])\n",
    "                c.add(m[j][i])\n",
    "            if len(r) != n or len(c) != n:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        n=len(matrix)\n",
    "        row=[0]*n\n",
    "        col=[0]*n\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                row[i]|=(1<<matrix[i][j])\n",
    "                col[j]|=(1<<matrix[i][j])\n",
    "        for r in row:\n",
    "            if ((1<<(n+1))-1)^r!=1:\n",
    "                return False\n",
    "        for c in col:\n",
    "            if ((1<<(n+1))-1)^c!=1:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        def transpose(matrix: list) -> list:\n",
    "            res = [[0]*len(matrix) for i in range(len(matrix[0]))]\n",
    "            for i in range(len(matrix)):\n",
    "                for j in range(len(matrix[0])):\n",
    "                    res[j][i] = matrix[i][j]\n",
    "            return res\n",
    "        def check(matrix):\n",
    "            for i in range(len(matrix)):\n",
    "                if len(set(matrix[i]))!=len(matrix):return False\n",
    "            return True\n",
    "        return check(matrix) and check(transpose(matrix))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        ls = set()\n",
    "        n = len(matrix)\n",
    "        for i in range(0,n):\n",
    "            for j in range(0,n):\n",
    "                ls.add(matrix[i][j])\n",
    "            if len(ls) != n:\n",
    "                return False\n",
    "            ls.clear()\n",
    "\n",
    "\n",
    "        ls.clear()\n",
    "        for i in range(0,n):\n",
    "            for j in range(0,n):\n",
    "                ls.add(matrix[j][i])\n",
    "            if len(ls) != n:\n",
    "                return False\n",
    "            ls.clear()\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        n=len(matrix)\n",
    "        nums=[i for i in range(1,n+1)]\n",
    "        # print(nums)\n",
    "        for line in matrix:\n",
    "            # if line.sort()!=nums:\n",
    "            if len(set(line))!=n:\n",
    "                return False\n",
    "        # print(line)\n",
    "        for i in range(n):\n",
    "            ans=[]\n",
    "            for j in range(n):\n",
    "                ans.append(matrix[j][i])\n",
    "            # print(ans)\n",
    "            if len(set(ans))!=n:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        # arr = [i+1 for i in len(matrix)]\n",
    "        n =  len(matrix)\n",
    "        row = [\n",
    "            [0 for __ in range(n)] for _ in range(n)\n",
    "        ]\n",
    "        colnum = [\n",
    "            [0 for __ in range(n)] for _ in range(n)\n",
    "        ]\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                row[i][matrix[i][j] -1] = 1\n",
    "                colnum[j][matrix[i][j] -1] = 1\n",
    "        for i in range(n):\n",
    "            for j in range(n):\n",
    "                if colnum[i][j] != 1 or row[i][j] != 1:\n",
    "                    return False\n",
    "        return True\n",
    "        # for i in range(len(matrix)):\n",
    "        #     ans = [i+1 for i in range(len(matrix))]\n",
    "        #     for j in range(len(matrix)):\n",
    "        #         if matrix[i][j] in ans:\n",
    "        #             ans.remove(matrix[i][j])\n",
    "        #     if len(ans)==0:\n",
    "        #         continue\n",
    "        #     else:\n",
    "        #         return False \n",
    "        \n",
    "        # for i in range(len(matrix)):\n",
    "        #     ans = [i+1 for i in range(len(matrix))]\n",
    "        #     for j in range(len(matrix)):\n",
    "        #         if matrix[j][i] in ans:\n",
    "        #             ans.remove(matrix[j][i])\n",
    "        #     if len(ans)==0:\n",
    "        #         continue\n",
    "        #     else:\n",
    "        #         return False "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def checkValid(self, matrix: List[List[int]]) -> bool:\n",
    "        n=len(matrix)\n",
    "        aa=0\n",
    "        bb=0\n",
    "        matrix2=[[matrix[i][j] for i in range(n)]for j in range(n)]\n",
    "        for i in range(n):\n",
    "            for j in range(1,n+1):\n",
    "                if j not in matrix[i]:\n",
    "                    aa=1\n",
    "                if j not in matrix2[i]:\n",
    "                    bb=1\n",
    "        if aa==0 and bb==0:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "        \n",
    "        \n",
    "        \n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
